<template>
    <div class="user-profile">
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>个人中心</span>
                </div>
            </template>
            <div class="profile-content">
                <div class="info-item">
                    <label>头像:</label>
                    <el-avatar :src="userInfo.avatarUrl" />
                </div>
                <div class="info-item">
                    <label>昵称:</label>
                    <span>{{ userInfo.nickname }}</span>
                </div>
                <div class="info-item">
                    <label>邮箱:</label>
                    <span>{{ userInfo.email }}</span>
                </div>
                <div class="info-item">
                    <label>手机号:</label>
                    <span>{{ userInfo.phone }}</span>
                </div>
                <div class="info-item">
                    <label>角色:</label>
                    <template v-if="userInfo.rolesName && userInfo.rolesName.length > 0">
                        <el-tag v-for="role in userInfo.rolesName" :key="role" type="success" class="role-tag">
                            {{ role }}
                        </el-tag>
                    </template>
                    <span v-else class="no-role">无角色</span>
                </div>
                <div class="info-item">
                    <label>状态:</label>
                    <el-tag :type="userInfo.isActived ? 'success' : 'danger'">
                        {{ userInfo.isActived ? '已激活' : '未激活' }}
                    </el-tag>
                </div>
                <div class="info-item">
                    <label>注册时间:</label>
                    <span>{{ formatDate(userInfo.createdAt) }}</span>
                </div>
                <div class="info-item">
                    <label>更新时间:</label>
                    <span>{{ formatDate(userInfo.updatedAt) }}</span>
                </div>
                <div class="info-item">
                    <label>备注:</label>
                    <span>{{ userInfo.remarks }}</span>
                </div>
            </div>
        </el-card>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useUserStore } from '../stores/user'
import { authApi } from '../api/auth'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const userInfo = ref({})

function formatDate(date) {
    if (!date) return ''
    return new Date(date).toLocaleString()
}

async function fetchUserInfo() {
    try {
        const res = await authApi.getUserInfo(userStore.userId)
        if (res.data.code === 1000) {
            userInfo.value = res.data.data
        } else {
            ElMessage.error(res.data.msg || '获取用户信息失败')
        }
    } catch {
        ElMessage.error('获取用户信息失败')
    }
}

// 监听头像更新事件
const handleAvatarUpdated = () => {
    fetchUserInfo()
}

onMounted(() => {
    fetchUserInfo()
    // 添加事件监听器
    window.addEventListener('avatar-updated', handleAvatarUpdated)
})

onUnmounted(() => {
    // 移除事件监听器
    window.removeEventListener('avatar-updated', handleAvatarUpdated)
})
</script>

<style scoped>
.user-profile {
    max-width: 600px;
    margin: 20px auto;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.profile-content {
    padding: 20px 0;
}

.info-item {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
    padding: 10px 0;
    border-bottom: 1px solid #f0f0f0;
}

.info-item label {
    font-weight: bold;
    width: 100px;
    color: #606266;
}

.info-item span {
    margin-left: 10px;
    color: #303133;
}

.role-tag {
    margin-right: 5px;
}

.no-role {
    color: #c0c4cc;
    font-style: italic;
}
</style>